#ifndef MEDIAWIN_H
#define MEDIAWIN_H
#include "media/branch/MediaBranchAC.h"
#include "media/branch/MediaBranchBT.h"
#include "media/branch/MediaBranchRT.h"
#include "media/branch/MediaBranchTT.h"
#include "media/branch/MediaBranchLF.h"
#include <QNetworkAccessManager>
#include <QtConcurrent/QtConcurrent>
#include <QGraphicsVideoItem>
#include <QNetworkRequest>
#include <QNetworkReply >
#include <QtConcurrent/QtConcurrent>
#include <QSslSocket>
#include "solar/trunk/NWidget.h"
#include "order/PowerStationPool.h"
#include "order/MultinworkQuery.h"
#include "intend/IceSailing.h"
#include "tools/Pyscript.h"
#include "tools/Wings.h"
#include "tools/Gadget.h"
#include <QtCore/Qt>
#include <QtWidgets>
#include <QSqlRecord>
#include <QSqlError>
#define TABRSASSERT if(RT_mediaTabWidget->rowCount() - 1 >= 0 && impact_int["mdrs-indexof"] >=0 && RT_mediaTabWidget->rowCount() - 1 >= impact_int["mdrs-indexof"])
#define TABRSLOOP for(int row = 0; row < RT_mediaTabWidget->rowCount(); row++)
#define media_type_music_assert if (impact_identify["mdrs-table-type"] == NT::CUT_MUSIC_TYPE)
#define media_type_movies_assert if (impact_identify["mdrs-table-type"] == NT::CUT_VIDEO_TYPE)
#define LOCAL_OWNER "SUPREMACY"
#define NEARPL_STALE_DATED 2
#define FAVORA_ITEM_LOGO_FLAG "CSTLI-ITEM-LOGOICO"
#define FAVORA_ITEM_RENAME_FLAG "CSTLI-ITEM-RENAME"
#define SEARCHFUNC_INIT (1994031720)
#define SEARCHFUNC_TRYLST (1994031721)
#define SEARCHFUNC_DLMDRS (1994031722)
#define SEARCHFUNC_VIEWBACK (1994031723)

class MediaWin :
        public NMainWindow,
        public MediaBranchAC,
        public MediaBranchBT,
        public MediaBranchCT,
        public MediaBranchTT,
        public MediaBranchLF
{

    Q_OBJECT

public:
    MediaWin(QMap<QString, QMap<QString, QString>> &RGLRLSMAP,
             QSqlDatabase *_DATABASE_,
             QMap<QString, QString> &QSSMAP);

    typedef enum {
        UnSwapKey,
        PreviousMedia,
        NextMedia
    } PlayerDFswapKey;

    typedef enum {
        dark,
        shallow
    } theme;

    ~MediaWin();

    QList<QString> format_video = {"mp4", "avi", "flv", "vob", "mov", "mkv", "m4v", "3gp", "rm", "rmvb", "wmv", "asf", "epv"};

    QList<QString> format_overall = {"mp3", "wav", "wma", "flac", "ape", "mp4", "avi", "flv", "vob", "mov", "m4v", "3gp", "rm", "rmvb", "wmv", "asf", "epv", "mkv" };

    QList<QString> format_image = {"bmp", "jpg", "png", "tif", "gif", "pcx", "tga", "exif", "fpx", "svg", "psd", "cdr", "pcd", "dxf", "ufo", "eps", "ai", "raw", "WMF", "webp", "avif", "apng", "ico" };

    QList<QString> foarmat_lyric = {"lrc", NULL, "txt"};

    QList<QString> __slots__ = {"database", "config_dict"};

    QString LoopModel = ":/loop-play.png";
    QString OncePlayModel = ":/once-play.png";
    QString OnceLoopModel = ":/onceloop-play.png";
    QString RandomModel = ":/random-play.png";
    QString DescModel = ":/desc-play.png";

    QTimer GLOBAL_SYSTEM_QUIT_QTIER;
    QTimer GLOBAL_ARGUR_QTIER;
    QTimer MULTI_VOLUME_QTIER;
    QTimer MULTI_DLPL_QTIER;
    QTimer USER_DLTXTC_QTIER;
    QTimer USER_DLLD_QTIER;
    QTimer TAB_DLLD_QTIER;

    QMap<QString, QVariant> impact_vari;
    QMap<QString, QString> impact_str;
    QMap<QString, bool> impact_swtc;
    QMap<QString, qint64> impact_int;
    QMap<ThreadPoint, NT::THREAD_STATE> impact_thread;
    QMap<QString, QList<QString>> impact_ststr;
    QMap<QString, QList<qint64>> impact_stint;
    QMap<QString, QList<bool>> impact_stbool;
    QMap<QString, QFont> impact_font;
    QMap<QString, QMap<QString, QString>> *__RLMAP__;
    QMap<QString, QString> *__QSSMAP__;
    QMap<QString, NT::CUT_MEDIA_TYPE> impact_identify;

    Nwidget *RectOverallWidget = new Nwidget(this);
    QGridLayout *RectOverallGrid = new QGridLayout(RectOverallWidget);

protected:

    QSqlDatabase *DATABASE;
    QSqlQuery *Query;
    DialPool *BoilPool;
    Nlwingsing wingsing;
    Extend definfc;
    MOUSE_ATTRIBUTE MSEVKEY;
    KEY_ATTRIBUTE RPLKEY;
    ResdownLoad Request;
    MEDIAPLMD mediaModeType;
    USER_DATA_TAKEIN UserDataCore;
    SecTabload *HpforTabLoader = new SecTabload(impact_thread);
    WaitaMinit *WaitTimer = new WaitaMinit;

    template<typename dtmap, typename _struct>
    void user_template(dtmap map, _struct &receive, QList<QString> impuritykey={}){
        if (!impuritykey.isEmpty()) {foreach(QString key, impuritykey) map.remove(key);}
        int indexof = -1;
        QString input_lgin_user = Login_idLibox->g_linedit()->text();
        QString input_lgin_psw = Login_keyLine->text();
        QString input_rgst_user = Register_idLine->text();
        QString input_rgst_psw = Register_keyLine->text();
        QString input_rgst_qpsw = Register_QkeyLine->text();
        QString input_rgst_alias = Register_aliasLine->text();
        QString input_datum_user = Datum_idLine->text();
        QString input_datum_psw = Datum_keyLine->text();
        QString input_datum_qpsw = Datum_QkeyLine->text();
        QString input_datum_alias = Datum_aliasEdit->text();
        if (!map["ac_id"].isEmpty() && map["tab-struct"].contains("ac_id")) receive.USER_IDS = map["ac_id"];
        if (!map["ac_name"].isEmpty() && map["tab-struct"].contains("ac_name")) receive.USER_NAMES = map["ac_name"];
        if (!map["ac_key"].isEmpty() && map["tab-struct"].contains("ac_key")) receive.USER_KEYS = map["ac_key"];
        if (!map["ac_head"].isEmpty() && map["tab-struct"].contains("ac_head")) receive.USER_HEAD_BTS = map["ac_head"];
        if (!map["ac_mdfvr"].isEmpty() && map["tab-struct"].contains("ac_mdfvr")) receive.USER_FAVOR_NS = map["ac_mdfvr"];
        if (!map["head_clipsize"].isEmpty() && map["tab-struct"].contains("head_clipsize")) receive.USER_HEAD_SZS = map["head_clipsize"];
        if (!map["head_cliprect"].isEmpty() && map["tab-struct"].contains("head_cliprect")) receive.USER_HEAD_RCS = map["head_cliprect"];
        if (!map["ac_headp"].isEmpty() && map["tab-struct"].contains("ac_headp")) receive.USER_HEAD_PS = map["ac_headp"];
        if (!map["ac_mdfvr"].isEmpty() && map["tab-struct"].contains("ac_mdfvr")) receive.USER_FAVOR_NS = map["ac_mdfvr"];
        if (!map["coverp"].isEmpty() && map["tab-struct"].contains("coverp")) receive.USER_FAVOR_PS = map["coverp"];
        if (!map["lock"].isEmpty() && map["tab-struct"].contains("lock")) receive.USER_FAVOR_LKS = map["lock"];
        if (!map["recall"].isEmpty() && map["tab-struct"].contains("recall")) receive.USER_RECALLS = definfc.isBoolli(map["recall"]);
        if (!map["relogin"].isEmpty() && map["tab-struct"].contains("relogin")) receive.USER_RELOGINS = definfc.isBoolli(map["relogin"]);
        if (!map["final"].isEmpty() && map["tab-struct"].contains("final")) receive.USER_FINALS = definfc.isBoolli(map["final"]);
        if (!map["might"].isEmpty() && map["tab-struct"].contains("might")) receive.USER_MIGHTS = definfc.isBoolli(map["might"]);
        if (!map["apply"].isEmpty() && map["tab-struct"].contains("apply")) receive.USER_HEAD_USES = definfc.isBoolli(map["apply"]);
        for(int _keyindex=0; _keyindex < map["ac_key"].size(); _keyindex++)
        {
            receive.USER_KEYS[_keyindex] = definfc.SuperConver(receive.USER_KEYS[_keyindex], cvType::cd_String);
        }
        if (!map["might"].isEmpty() && map["might"].contains("1"))
        {
            indexof = map["might"].indexOf("1");
        }
        else if ((!map["final"].isEmpty() && map["final"].contains("1")))
        {
            indexof = map["final"].indexOf("1");
        }
        if (indexof != -1)
        {
            receive.USER_ID = map["ac_id"][indexof];
            receive.USER_NAME = map["ac_name"][indexof];
            receive.USER_KEY = receive.USER_KEYS[indexof];
            receive.USER_HEAD = map["ac_head"][indexof];
            receive.USER_RECALL = static_cast<bool>(map["recall"][indexof].toUInt());
            receive.USER_RELOGIN = static_cast<bool>(map["relogin"][indexof].toUInt());
            receive.USER_FINAL = static_cast<bool>(map["final"][indexof].toUInt());
            receive.USER_MIGHT = static_cast<bool>(map["might"][indexof].toUInt());
        }
        if (map["apply"].contains("1"))
        {
            indexof = map["apply"].indexOf("1");
            receive.USER_HEADP = map["ac_headp"][indexof];
            receive.USER_HEAD_SZ = map["head_clipsize"][indexof];
            receive.USER_HEAD_RC = map["head_cliprect"][indexof];
            receive.USER_HEAD_USE = map["apply"][indexof];
        }
        receive.IP_LOGIN_NAME = input_lgin_user;
        receive.IP_LOGIN_KEY = input_lgin_psw;
        receive.IP_ENROLL_NAME = input_rgst_user;
        receive.IP_ENROLL_KEY = input_rgst_psw;
        receive.IP_ENROLL_QKEY = input_rgst_qpsw;
        receive.IP_ENROLL_ALIAS = input_rgst_alias;
        receive.IP_DATUM_NAME = input_datum_user;
        receive.IP_DATUM_KEY = input_datum_psw;
        receive.IP_DATUM_QKEY = input_datum_qpsw;
        receive.IP_DATUM_ALIAS = input_datum_alias;
        receive.IP_LOGIN_RECALL = Login_fillkeycBox->isChecked();
        receive.IP_LOGIN_RELOGIN = Login_autoLogincBox->isChecked();

    }

    void themeset(theme _theme_)
    {
        if (_theme_ == theme::dark)
        {
            /* bottom dark config */
            MT_playMdStyle.insert(MEDIAPLMD::PLMD_LOOP,     ":/PIXMAP/bench/plmd-loop-dark.png");
            MT_playMdStyle.insert(MEDIAPLMD::PLMD_DESC,     ":/PIXMAP/bench/plmd-desc-dark.png");
            MT_playMdStyle.insert(MEDIAPLMD::PLMD_RANDOM,   ":/PIXMAP/bench/plmd-random-dark.png");
            MT_playMdStyle.insert(MEDIAPLMD::PLMD_ONCE,     ":/PIXMAP/bench/plmd-once-dark.png");
            MT_playMdStyle.insert(MEDIAPLMD::PLMD_ONCELOOP, ":/PIXMAP/bench/plmd-onceloop-dark.png");
            MT_mdplermode->doconfigur_pixmap(MT_playMdStyle[MEDIAPLMD::PLMD_LOOP]);

            MT_volumeStyle.insert(MEDIAVOLUME::VOLUME_UP,    ":/PIXMAP/bench/volume-up-dark.png");
            MT_volumeStyle.insert(MEDIAVOLUME::VOLUME_DOWM,  ":/PIXMAP/bench/volume-down-dark.png");
            MT_volumeStyle.insert(MEDIAVOLUME::VOLUME_NOTICE,":/PIXMAP/bench/volume-notice-dark.png");
            MT_volumeStyle.insert(MEDIAVOLUME::VOLUME_MUTE,  ":/PIXMAP/bench/volume-mute-dark.png");
            MT_volumeStyle.insert(MEDIAVOLUME::VOLUME_SMALL, ":/PIXMAP/bench/volume-small-dark.png");
            if (50 <= VIDEO_VOLUME)
            {
                MT_volumeView->doconfigur_pixmap(MT_volumeStyle[MEDIAVOLUME::VOLUME_NOTICE]);
            } else {
                MT_volumeView->doconfigur_pixmap(MT_volumeStyle[MEDIAVOLUME::VOLUME_SMALL]);
            }

            MT_screen->doconfigur_pixmap(":/PIXMAP/bench/full-screen-dark.png");
            MT_speedView->doconfigur_colorgroup({{MSEVKEY.WIN_NORMAL, QColor(116,124,141,230)}});

            MT_mdplermode->doconfigur_backcolorgroup({{MSEVKEY.WIN_ENTER, QColor(222,225,232)}});
            MT_volumeView->doconfigur_backcolorgroup({{MSEVKEY.WIN_ENTER, QColor(222,225,232)}});
            MT_speedView->doconfigur_backcolorgroup({{MSEVKEY.WIN_ENTER, QColor(222,225,232)}});
            MT_screen->doconfigur_backcolorgroup({{MSEVKEY.WIN_ENTER, QColor(222,225,232)}});
            /* captionbar dark config */
            AC_userBar->addAction(QIcon(":/PIXMAP/bench/captionbar-register-dark.png"), "注册");
            AC_userBar->addSeparator();
            AC_userBar->addAction(QIcon(":/PIXMAP/bench/captionbar-unlogin-dark.png"),  "退出登录");
            AC_userBar->setStyleSheet("QToolBar QToolButton:hover{background-color:rgba(231,225,228,180);}");
        }
        else if (_theme_ == theme::shallow)
        {
            /* bottom shallow config */
            MT_playMdStyle.insert(MEDIAPLMD::PLMD_LOOP,     ":/PIXMAP/bench/plmd-loop-shallow.png");
            MT_playMdStyle.insert(MEDIAPLMD::PLMD_DESC,     ":/PIXMAP/bench/plmd-desc-shallow.png");
            MT_playMdStyle.insert(MEDIAPLMD::PLMD_RANDOM,   ":/PIXMAP/bench/plmd-random-shallow.png");
            MT_playMdStyle.insert(MEDIAPLMD::PLMD_ONCE,     ":/PIXMAP/bench/plmd-once-shallow.png");
            MT_playMdStyle.insert(MEDIAPLMD::PLMD_ONCELOOP, ":/PIXMAP/bench/plmd-onceloop-shallow.png");
            MT_mdplermode->doconfigur_pixmap(MT_playMdStyle[MEDIAPLMD::PLMD_LOOP]);

            MT_volumeStyle.insert(MEDIAVOLUME::VOLUME_UP,    ":/PIXMAP/bench/volume-up-shallow.png");
            MT_volumeStyle.insert(MEDIAVOLUME::VOLUME_DOWM,  ":/PIXMAP/bench/volume-down-shallow.png");
            MT_volumeStyle.insert(MEDIAVOLUME::VOLUME_NOTICE,":/PIXMAP/bench/volume-notice-shallow.png");
            MT_volumeStyle.insert(MEDIAVOLUME::VOLUME_MUTE,  ":/PIXMAP/bench/volume-mute-shallow.png");
            MT_volumeStyle.insert(MEDIAVOLUME::VOLUME_SMALL, ":/PIXMAP/bench/volume-small-shallow.png");

            MT_screen->doconfigur_pixmap(":/PIXMAP/bench/full-screen-shallow.png");
            MT_speedView->doconfigur_colorgroup({{MSEVKEY.WIN_NORMAL, QColor(116,124,141,230)}});

            /* captionbar shallow config */
            AC_userBar->addAction(QIcon(":/PIXMAP/bench/captionbar-register-shallow.png"), "注册");
            AC_userBar->addSeparator();
            AC_userBar->addAction(QIcon(":/PIXMAP/bench/captionbar-unlogin-shallow.png"),  "退出登录");
            AC_userBar->setToolButtonStyle(Qt::ToolButtonFollowStyle);
            AC_userBar->setIconSize(QSize(24,27));
            AC_userBar->setStyleSheet("QToolBar QToolButton:hover{background-color:rgba(231,225,228,180);}");
        }
    }


private:
    QStringList ExcelCol;
    QString NormalBsc;
    QString MinWinBsc;
    QString MaxWinBsc;
    QString QuitWinBsc;

signals:
    void sg_fromtype_amend(NT::CUT_MEDIA_TYPE, QString="");
    void sg_standar_amend(bool);
    void sg_alldata_query();
    void sg_userinfo_quit();
    void sg_system_quit(bool, NT::WIN_QUIT_ATTRIBUTE);
    void sg_favoradata_load(QString, bool, QString);
};
#endif

